import numpy as np
import csv
import random
from datetime import datetime
from time import strftime

'''
Global and Local Empirical Bayes Smoothers with Gamma Model
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def contiguity_item(ID, contiguigy):
    i = 0
    ROW = contiguigy.shape[0]
    #print ROW
    contiguityItem = []
    j = 0
    
    while i < ROW:
        if contiguigy[i,0]== ID:
            contiguityItem.append(contiguigy[i,1])
            j = j + 1
        i = i + 1
    if j == 1:
        print ID, "does not have contiguity items."
    #elif j == 
    return contiguityItem

def contiguityHashmapItem(ID, contiguigy):
    temp = contiguigy[ID]
    temp = temp.split(',')
    contiguityItem = []
    #print temp
    for item in temp:
        if int(item) <> ID:
            contiguityItem.append(int(item)+1)
    return contiguityItem

def nearestItemWithPopConstraint(ID, contiguigy, denominator, popConstraint, centroids):
    tempSum = denominator[ID];
    conItemList = [ID]
    searchSet = set()
    curID = ID
    curCentroid = centroids[ID,:]    

    while(tempSum < popConstraint):
        tempMinDist = 100000000000
        tempConitguityItem = contiguityHashmapItem(curID, contiguigy)
        for i in tempConitguityItem:
            if((i not in conItemList)):
                searchSet.add(i)

        for i in searchSet:
            tempDist = ((centroids[i,0] - curCentroid[0])**2 + (centroids[i,1] - curCentroid[1])**2)**0.5

            if tempDist < tempMinDist:
                tempMinDist = tempDist
                tempMinID = i
                tempMinpop = denominator[i]

        tempSum = tempSum + tempMinpop
        conItemList.append(tempMinID)
        #print searchSet, tempMinID
        searchSet.remove(tempMinID)
        curID = tempMinID

    return conItemList
#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()
    #unitCSV = 'C:/_DATA/migration89_08/COUNTY Migration/SaTScan/exclude_nearest/county.csv'
    unitCSV = 'C:/Users/Hu/Desktop/GeoBugs_example data/satscan_disease.csv'
    contiguityCSV = 'C:/Users/Hu/Desktop/GeoBugs_example data/NortheeaternUS_re_contiguity.ctg'
    #centroidsCSV = 'C:/_DATA/migration89_08/COUNTY Migration/SaTScan/exclude_nearest/centroids.csv'
    dataMatrix = build_data_list(unitCSV)
    spContiguity = build_data_list(contiguityCSV)
    #print spContiguity
    
    #centroids = build_data_list(centroidsCSV)
    contiguityHashmap = {}
    #excludeIDList = [271, 1259, 2562, 2874, 3080, 3106]
    tempID = np.zeros(max(dataMatrix[:,0])+1)
    for item in tempID:
        item = -1
    i = 0
    for item in dataMatrix[:,0]:
        tempID[int(item)] = i
        i = i + 1
    #print dataMatrix
    for item in spContiguity:
        #print int(item[0]), int(item[1])
        if (tempID[int(item[0])] > -1) and (tempID[int(item[1])] > -1):
            temp = ''
            if int(item[0]) in contiguityHashmap:
                temp = contiguityHashmap[int(item[0])] + ','
            contiguityHashmap[int(item[0])] = temp + str(int(tempID[int(item[1])]))

    output = ''
    for item in contiguityHashmap:
        #print contiguityHashmapItem(item, contiguityHashmap)
        output += str(len(contiguityHashmapItem(item, contiguityHashmap)))
        output += ', '
    print output

    a = [3, 4, 
6, 19, 3, 
2, 1, 4, 
5, 1, 3, 17, 23, 
4, 23, 
19, 7, 2, 
8, 6, 30, 22, 28, 
24, 7, 9, 28, 
16, 8, 12, 
11, 18, 13, 
12, 15, 10, 18, 
11, 9, 16, 15, 
10, 20, 14, 
20, 13, 206, 
21, 18, 11, 12, 
29, 24, 9, 12, 21, 
19, 4, 25, 
20, 21, 27, 10, 11, 15, 
6, 22, 2, 26, 17, 33, 
14, 13, 31, 27, 38, 18, 
16, 29, 15, 27, 18, 
33, 30, 19, 7, 
4, 5, 
16, 8, 40, 28, 29, 41, 39, 
26, 17, 
33, 25, 19, 
34, 21, 20, 18, 35, 
39, 8, 36, 42, 7, 24, 
34, 16, 21, 24, 
33, 7, 36, 22, 
38, 20, 45, 32, 
53, 31, 45, 50, 208, 210, 
19, 30, 26, 22, 
35, 41, 47, 29, 27, 
34, 44, 27, 37, 
28, 30, 42, 
43, 44, 38, 35, 51, 
20, 31, 43, 37, 
24, 40, 28, 42, 
46, 48, 24, 39, 41, 
34, 49, 24, 40, 
28, 46, 36, 39, 
51, 52, 45, 37, 38, 
35, 51, 37, 47, 
43, 32, 31, 50, 
40, 42, 54, 48, 
58, 49, 44, 34, 
59, 49, 40, 46, 
41, 47, 60, 48, 
55, 32, 52, 45, 
56, 43, 37, 57, 44, 63, 
43, 56, 50, 55, 
55, 226, 62, 32, 
46, 59, 
56, 50, 53, 52, 62, 64, 
64, 55, 52, 51, 
63, 73, 51, 66, 58, 61, 
57, 61, 60, 47, 
68, 60, 48, 54, 65, 
78, 76, 49, 66, 58, 59, 
66, 58, 57, 
53, 55, 77, 64, 233, 69, 79, 
57, 71, 51, 64, 
55, 69, 56, 62, 63, 
59, 68, 
57, 61, 60, 74, 75, 
68, 70, 72, 
65, 70, 59, 67, 
64, 77, 90, 71, 62, 
78, 80, 83, 67, 68, 
98, 63, 73, 69, 
101, 67, 
88, 84, 74, 57, 71, 
73, 87, 75, 66, 88, 
82, 74, 66, 76, 
82, 78, 60, 81, 75, 
69, 79, 95, 62, 90, 
81, 70, 60, 80, 76, 
89, 240, 95, 62, 77, 
78, 70, 
78, 86, 76, 
76, 75, 87, 86, 
70, 
88, 98, 106, 73, 
237, 97, 103, 240, 89, 
81, 82, 
74, 88, 82, 
73, 87, 84, 74, 
240, 103, 85, 79, 
107, 98, 77, 108, 69, 
93, 105, 241, 111, 94, 
99, 243, 102, 115, 93, 
102, 92, 239, 91, 
100, 91, 242, 241, 
112, 79, 107, 77, 
97, 118, 238, 100, 109, 
85, 237, 96, 
90, 106, 71, 84, 
120, 92, 243, 
110, 94, 242, 96, 
72, 104, 
92, 93, 116, 105, 
119, 109, 89, 85, 
101, 
111, 102, 127, 91, 
84, 98, 108, 126, 
113, 90, 123, 122, 95, 
106, 90, 121, 
125, 96, 103, 114, 117, 
111, 133, 100, 118, 141, 140, 
127, 143, 91, 110, 105, 
122, 124, 119, 95, 
123, 121, 107, 129, 
125, 118, 109, 149, 130, 117, 
92, 132, 148, 120, 116, 
127, 102, 115, 147, 
114, 109, 
110, 130, 114, 96, 
125, 112, 103, 
115, 132, 99, 
134, 126, 131, 108, 113, 
112, 137, 124, 107, 123, 
144, 107, 129, 113, 122, 139, 
112, 122, 136, 
109, 114, 119, 146, 
106, 131, 135, 121, 
116, 147, 111, 105, 143, 
135, 138, 
123, 134, 113, 144, 
114, 118, 133, 145, 
134, 126, 121, 
115, 148, 120, 
130, 145, 110, 141, 
131, 121, 129, 
142, 128, 126, 
124, 152, 146, 
158, 122, 139, 152, 
128, 
123, 144, 153, 137, 158, 
159, 143, 110, 141, 
133, 140, 168, 162, 110, 
135, 
127, 140, 111, 163, 
153, 150, 129, 123, 139, 
130, 133, 151, 
161, 164, 125, 157, 136, 154, 
127, 148, 169, 116, 163, 
115, 147, 155, 132, 
151, 114, 154, 
144, 
149, 145, 160, 
157, 170, 166, 158, 136, 137, 
158, 144, 139, 156, 
149, 146, 161, 
173, 148, 169, 
153, 158, 167, 
172, 164, 170, 146, 152, 
152, 156, 139, 137, 166, 153, 
168, 140, 180, 163, 
165, 151, 162, 171, 
165, 154, 146, 164, 
168, 179, 141, 171, 160, 
182, 169, 143, 159, 147, 
176, 146, 157, 161, 172, 181, 
160, 183, 171, 161, 
167, 158, 152, 178, 174, 
156, 166, 
179, 141, 162, 159, 
147, 173, 163, 155, 
174, 152, 172, 157, 
186, 160, 184, 165, 162, 
164, 157, 176, 170, 175, 
155, 169, 
170, 166, 178, 175, 
178, 177, 174, 172, 
181, 164, 189, 172, 
175, 187, 
187, 192, 175, 166, 174, 
186, 168, 162, 
182, 159, 
164, 183, 176, 
163, 180, 
181, 165, 185, 
186, 190, 185, 171, 
183, 190, 184, 244, 
171, 191, 179, 184, 
178, 192, 177, 
193, 
197, 195, 176, 
184, 194, 185, 191, 
196, 186, 190, 245, 
187, 178, 
188, 195, 
199, 196, 190, 
197, 201, 193, 189, 198, 
199, 194, 245, 191, 200, 
189, 204, 195, 202, 
195, 
196, 194, 
203, 196, 
202, 195, 
204, 205, 197, 201, 
200, 
202, 197, 205, 
204, 202, 
14, 208, 207, 
206, 209, 208, 
210, 209, 206, 207, 32, 
208, 210, 207, 211, 216, 
217, 32, 209, 208, 
216, 209, 215, 227, 224, 221, 
219, 213, 
218, 212, 214, 
218, 215, 213, 220, 222, 
221, 220, 214, 211, 
211, 209, 224, 217, 
216, 226, 210, 
223, 219, 222, 214, 213, 
223, 212, 231, 218, 
225, 221, 222, 215, 214, 
215, 220, 225, 211, 227, 
214, 218, 223, 228, 220, 232, 
219, 222, 218, 232, 
216, 233, 226, 211, 227, 
228, 220, 230, 221, 
224, 233, 217, 53, 
234, 224, 230, 211, 221, 
230, 225, 235, 232, 238, 222, 
239, 231, 236, 
235, 225, 227, 228, 
241, 219, 232, 229, 
228, 223, 242, 231, 222, 
240, 224, 226, 234, 62, 
237, 233, 235, 227, 
228, 237, 234, 230, 
243, 229, 
240, 97, 238, 85, 235, 234, 
96, 228, 242, 237, 
93, 229, 243, 241, 
233, 85, 79, 89, 237, 
231, 94, 239, 91, 242, 
238, 100, 241, 94, 232, 
92, 236, 99, 239, 
185, 
191, 196]

    print len(a)

    print "end at " + getCurTime()
    print "==========================="

           
